package com.lehecai.core.task.sender.validator;

import com.lehecai.core.task.IAsyncTask;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by qatang on 14-3-26.
 */
public abstract class AbstractAsyncTaskSenderValidator implements IAsyncTaskSenderValidator {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean validate(IAsyncTask asyncTask) throws Exception {
        if (asyncTask == null) {
            throw new Exception("验证单一异步任务失败：asyncTask参数对象不能为空");
        }

        if (!validateTask(asyncTask)) {
            String errorMsg = asyncTask.getAsyncTaskType().getName();
            if (!StringUtils.isEmpty(asyncTask.getName())) {
                errorMsg = asyncTask.getName();
            }
            logger.error(errorMsg + " 认证失败，一票否决制验证失败");
            return false;
        }
        return true;
    }

    protected abstract boolean validateTask(IAsyncTask asyncTask) throws Exception;
}
